package com.ctd.springframework.util;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Objects;

/**
 * ResourceUtils
 *
 * @author chentudong
 * @time 2019/3/24 14:11
 */
public abstract class ResourceUtils {
    //PREFIX
    public static final String CLASSPATH_URL_PREFIX = "classpath:";
    public static final String FILE_URL_PREFIX = "file:";
    public static final String JAR_URL_PREFIX = "jar:";
    public static final String WAR_URL_PREFIX = "war:";
    public static final String URL_PROTOCOL_FILE = "file";
    public static final String JAR_URL_SEPARATOR = "!/";
    public static final String WAR_URL_SEPARATOR = "*/";

    /**
     * assert classpath:
     * is null to new Url(resourceLocation);
     *
     * @param resourceLocation resourceLocation
     * @return boolean
     */
    public static boolean isUrl(String resourceLocation) {
        if (Objects.isNull(resourceLocation)) {
            return false;
        }
        //assert classpath: startWith
        if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) {
            return true;
        }
        //create new url
        try {
            new URL(resourceLocation);
            return true;
        } catch (MalformedURLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
